package com.myh.cc.coom.ranking.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.myh.cc.R
import com.myh.cc.coom.ranking.bean.RankingDetailsBean
import com.myh.cc.databinding.ItemRankingDetailsBinding
import com.myh.cc.widget.OnItemClickListener

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/18.   ：
 */
class RankingDetailsAdapter(bean: RankingDetailsBean, context: Context): RecyclerView.Adapter<RankingDetailsAdapter.VH>() {


    val bean = bean
    val context = context
    var  singers :String = ""
    var mListener: OnItemClickListener? = null

    override fun onBindViewHolder(holder: RankingDetailsAdapter.VH, position: Int) {
        var  list = bean.songlist[position]

        when(position){
            0 ->  holder.binding.itemDetailsTvTop.setTextColor(context.resources.getColor(R.color.top_one))
            1 ->  holder.binding.itemDetailsTvTop.setTextColor(context.resources.getColor(R.color.top_two))
            2 ->  holder.binding.itemDetailsTvTop.setTextColor(context.resources.getColor(R.color.top_three))
        }

        singers = ""
        for (singer_name in list.data.singer)
              singers += singer_name.name + "/"
        // 使用 joinToString
        singers.substring(0,singers.length - 2)

        holder.binding.itemDetailsTvTop.text = (position + 1).toString()
        holder.binding.itemDetailsTvMusicName.text = list.data.songname
        holder.binding.itemDetailsTvAuthName.text = singers

        holder.binding.itemRelaRankingDetails.setOnClickListener{

            view ->
            mListener?.onClick(view,position)
        }

    }

    override fun getItemCount(): Int {
        return bean.songlist.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RankingDetailsAdapter.VH {
        return RankingDetailsAdapter.VH.create(LayoutInflater.from(parent.context),parent)
    }

    fun setOnItemClickListener(mListener: OnItemClickListener){
        this.mListener = mListener
    }

    class VH(binding: ItemRankingDetailsBinding): RecyclerView.ViewHolder(binding.root) {

        val binding = binding

        companion object {

            fun create(inflater: LayoutInflater, parent: ViewGroup): RankingDetailsAdapter.VH {

                val binding = ItemRankingDetailsBinding.inflate(inflater, parent, false)
                return RankingDetailsAdapter.VH(binding)
            }

        }
    }
}